{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Exercise (2). \n",
    "定义文件xx.tar.gz 的产生方式如下：\n",
    " - 以xx 为文件名的文件通过tar 和gzip 打包压缩产生，该文件中以字符串的方式记录\n",
    "了一个非负整数；\n",
    " - 或者以xx 为名的目录通过tar 和gzip 打包压缩产生，该目录中包含若干xx.tar.gz。\n",
    "其中，x 2 [0, 9]。  \n",
    "  现给定一个根据上述定义生成的文件00.tar.gz (该文件从课程网站\n",
    "下载)，请确定其中包含的以xx 为文件名的文件个数以及这些文件中所记录的非负整数之和。  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os,tarfile\n",
    "os.chdir(\"test\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# d={\"tar\":0,\"file\":0,\"adder\":0}\n",
    "def unpack_path_file(pathname):\n",
    "    archive = tarfile.open(pathname, 'r:gz')\n",
    "    path=os.path.split(os.path.abspath(pathname))[0]\n",
    "#     d[\"tar\"]+=1\n",
    "    f,n=0,0\n",
    "    for tarinfo in archive:\n",
    "        archive.extract(tarinfo, path)\n",
    "        name=tarinfo.name\n",
    "#         print(\"name:{name}\".format(**locals()))\n",
    "        tfname=os.path.join(path,tarinfo.name)\n",
    "        if tarinfo.isfile():\n",
    "            if tarinfo.name.rfind(\".tar.gz\")!=-1:\n",
    "                f1,n1=unpack_path_file(tfname)\n",
    "                f+=f1\n",
    "                n+=n1\n",
    "            else:\n",
    "                f+=1\n",
    "                with open(tfname) as fp:\n",
    "                    n+=int(fp.read())\n",
    "#         elif tarinfo\n",
    "    archive.close()\n",
    "    return f,n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3170, 15752491)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "unpack_path_file(\"00.tar.gz\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'tar': 6440, 'file': 3170, 'adder': 15752491}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "a=tarfile.open(r\"00\\01\\01.tar.gz\",'r:gz')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<TarInfo '01' at 0x1e29800a688>\n",
      "01\n",
      "{'name': '01', 'mode': 438, 'uid': 0, 'gid': 0, 'size': 4, 'mtime': 1543047064, 'chksum': 3816, 'type': b'0', 'linkname': '', 'uname': '', 'gname': '', 'devmajor': 0, 'devminor': 0}\n",
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "for t in a:\n",
    "    print(t)\n",
    "    print(t.name)\n",
    "    print(t.get_info())\n",
    "    print(t.isdir())\n",
    "    print(t.isfile())\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'E:\\\\UCAS\\\\算法概论\\\\AlgorithmExercise\\\\E02\\\\test'"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "os.path.split(os.path.abspath(\"00.tar.gz\"))[0]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"sfsfa\"[::-1].find(\"a\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
